<?php
exit;
// card id  pe2K2jueSlpaafyz1PB30pXpeInk
ini_set('display_errors', 'On');
define('APPID', 'wx3b97b20380656267');
define('SECRET', '');

//define('APPID', 'wxe214ac48790c622f');
//define('SECRET', '62d3091e10b72a50a8a2c4f758a799b2');//selim

function get_access_token(){
	$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".SECRET;
	$res = https_request($url, 'get', 'json');
	$access_token = $res["access_token"];
	return $access_token;
}
 
function https_request($url, $type="get", $res="json", $data = ''){
    //1.初始化curl
    $curl = curl_init();
    //2.设置curl的参数
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    if ($type == "post"){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    //3.采集
    $output = curl_exec($curl);
    //var_dump(curl_getinfo($curl));
    //4.关闭
    curl_close($curl);
    if ($res == 'json') {
        return json_decode($output,true);
    }
}
 
//上传图片素材(该方法只能获取到图片的URL)     2016-11-3
function addMaterialUrl() {
	$access_token = get_access_token();    
	$url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=".$access_token;
	$filename = 'test.png';  //文件名同目录下 其他目录自己指定
	$data = array("media"=>'@'. $filename);
	$res = https_request( $url ,'post', 'json', $data);
	print_r($res);
	return $res['url'];
}

$logo_url='http://mmbiz.qpic.cn/mmbiz_jpg/VDBKliauDV44ibw7xsZVFzoCFuI5qRUhOZBl8IjT6EvO2255ALk60x2U2XQlLwdupDX0LeQxnDTLHA0m5nrThicZA/0?wx_fmt=jpeg';
function createCardMeg($logo_url, $access_token) {

	
	$url = "https://api.weixin.qq.com/card/create?access_token=".$access_token;
	$data = '{
  "card": {
      "card_type": "GENERAL_COUPON",
      "general_coupon": {
          "base_info": {
              "logo_url":"'.$logo_url.'",
              "brand_name": "海信集团有限公司",
              "code_type": "CODE_TYPE_BARCODE",
              "title": "海信200元消费补贴",
              "color": "Color070",
              "notice": "进店消费记得出示给工作人员卡券哦",
              "description": "",
              "date_info": {
                  "type": "DATE_TYPE_FIX_TERM",
                  "fixed_term":30,
                  "fixed_begin_term":0
              },
              "sku": {
                  "quantity": 100000
              },
              "use_limit":1,
              "get_limit": 1,
              "use_custom_code": true,
              "bind_openid": false,
              "can_share": true,
              "can_give_friend": true 
          },
           "advanced_info": {
               
           },
           "default_detail":"1.此券仅限指定机型使用，适用于海信、容声、科龙等全国线下专柜；
2.此券可用于单笔一次性消费使用，每人限领一张；
3.活动时间：4月24日-5月31日。"
          
      }
  }}';
	$res = https_request( $url ,'post', 'json', $data);
	var_dump($res);
	return $res['card_id'];
}
 
 

 
//$access_token = get_access_token(); 



$appId='wx_zhiguang';
$appSecret='wn22d01hged73s82le';
$url='http://sweixinapi.hisense.com/openapi/token?grant_type=client_credential&appid='.$appId.'&secret='.$appSecret;
$data=json_decode(httpGet($url),true);
var_dump($data);
echo "<br />";
$hai_token=$data['access_token'];
$url='http://sweixinapi.hisense.com/interface/weChatExternal/qryAccessToken?access_token='.$hai_token;
$data=json_decode(httpGet($url),true);
var_dump($data);
$access_token=$data['data'];

createCardMeg($logo_url,$access_token);



function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
    
    $res = curl_exec($curl);
    curl_close($curl);
    var_dump($res);
    
    return $res;
}



 